package com.quanta.edaily.controller;

import com.alibaba.fastjson.JSONObject;
import com.quanta.edaily.Service.UserService;
import com.quanta.edaily.bean.JsonResponse;
import com.quanta.edaily.pojo.User;
import com.quanta.edaily.pojo.UserDTO;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Min;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/loginByEDailyId")
    public JsonResponse loginByEDailyId(@Validated @RequestBody UserDTO userDTO) {
        return userService.loginByEDailyId(userDTO.getEdailyId(), userDTO.getPassword());
    }

    @PostMapping("/register")
    public JsonResponse register(@Validated @RequestBody UserDTO userDTO) {
        return userService.register(userDTO.getName(), userDTO.getPhone(), userDTO.getPassword());
    }

    @PostMapping("/update")
    public JsonResponse update(@RequestBody User user) {
        return userService.updateUserById(user);
    }

    @PostMapping("/updatePasswordById")
    public JsonResponse updatePasswordById(@RequestBody JSONObject jsonObject) {
        Integer id = jsonObject.getInteger("id");
        String oldPassword = jsonObject.getString("oldPassword");
        String newPassword = jsonObject.getString("newPassword");
        return userService.updatePasswordById(id,oldPassword, newPassword);
    }

    @PostMapping("/deleteById/{id}")
    public JsonResponse deleteById(@PathVariable Integer id) {
            return userService.deleteUserById(id);
    }

    @GetMapping("/getUserById")
    public JsonResponse getUserById(@RequestParam Integer id) {
        return userService.getUserById(id);
    }

    @GetMapping("/getAll")
    public JsonResponse getAll() {
        return userService.getAll();
    }

    @GetMapping("/findPage")
    public JsonResponse findPage(@RequestParam @Min(1) int pageNum,
                                 @RequestParam @Min(1) int pageSize) {
        return userService.findPage((pageNum-1)*pageSize, pageSize);
    }

    @GetMapping("/countAllUser")
    public JsonResponse countAllUser() {
        return JsonResponse.success("查询成功",userService.countAllUser());
    }

    @GetMapping("/getPublished")
    public JsonResponse getPublished() {
        return userService.getPublished();
    }
}